package com.sl.common.trans;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.fhs.core.trans.anno.Trans;
import com.fhs.core.trans.vo.VO;
import com.fhs.trans.service.impl.SimpleTransService;
import lombok.SneakyThrows;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.*;

public class CustomSimpleTransService extends SimpleTransService {

    @SneakyThrows
    public CustomSimpleTransService(SimpleTransService service) {
        Object transDiver1 = ReflectUtil.getFieldValue(service, "transDiver");
        if(ObjectUtil.isNotNull(transDiver1)){
            this.regsiterTransDiver((SimpleTransDiver) transDiver1);
        }
        transDiver1 = ReflectUtil.getFieldValue(service, "simpleUnTransDiver");
        if(ObjectUtil.isNotNull(transDiver1)){
           ReflectUtil.setFieldValue(this,"simpleUnTransDiver",transDiver1);
        }

        transDiver1 = ReflectUtil.getFieldValue(service, "dataSourceSetter");
        if(ObjectUtil.isNotNull(transDiver1)){
            ReflectUtil.setFieldValue(this,"dataSourceSetter",transDiver1);
        }
        this.afterPropertiesSet();
    }

    @Override
    public List<? extends VO> findByIds(List ids, Trans tempTrans, Set<String> targetFields) {
        return findByIds(() -> {
            return transDiver.findByIds(ids, getTargetClass(tempTrans), tempTrans.uniqueField());
        }, tempTrans.dataSource());
    }


    /**
     * 根据id查询单个
     *
     * @param id
     * @param tempTrans
     * @return
     */
    public VO findById(Object id, Trans tempTrans) {
        return findById(() -> {
            HashSet<String> fields = new HashSet<>(Arrays.asList(tempTrans.fields()));
            if (transCacheSettMap.containsKey(getTargetClassName(tempTrans))) {
                fields = null;
            }
            return transDiver.findById((Serializable) id, getTargetClass(tempTrans), tempTrans.uniqueField(), fields);
        }, tempTrans.dataSource());
    }
    /**
     * 因为要兼容RPC Trans 所以这里这么写
     *
     * @param tempTrans tempTrans
     * @return
     */
    protected Class<? extends VO> getTargetClass(Trans tempTrans) {
        return ClassUtil.loadClass(this.getTargetClassName(tempTrans));
    }



    @Override
    public void transOne(VO obj, List<Field> toTransList) {
       super.transOne(obj,toTransList);
        Map<String, Object> transMap = obj.getTransMap();
        if(MapUtil.isNotEmpty(transMap)){
            Map<String,Object> stringMap = new LinkedHashMap<>();
            for (Map.Entry<String, Object> stringStringEntry : transMap.entrySet()) {

                String key = stringStringEntry.getKey();
                Object value = stringStringEntry.getValue();
                stringMap.put(key,value);
                if(StrUtil.containsIgnoreCase(key,"Break")){
                    stringMap.put(StrUtil.subBefore(key,"Break",true),value);
                    stringMap.remove(key);
                }
            }
            transMap.clear();
            transMap.putAll(stringMap);

        }

    }
}
